package leetcode_cn;

/**
 * 反转字符串中的单词3
 *
 * @author briarbear
 * @blog https://briarbear.github.io
 * @date 2018-09-07 16:45
 */
public class ReverseWords {


    public String solution(String s){
        if(s == null || s.length() <=1) return s;
        String[] strs = s.split(" ");

        StringBuilder res = new StringBuilder();
        res.append(reverse(strs[0]));
        for (int i = 1; i < strs.length; i++) {
            res.append(" ");
            res.append(reverse(strs[i]));
        }

        return res.toString();
    }

    public String reverse(String s){
        char[] chars = s.toCharArray();
        int len = chars.length;
        char[] res = new char[len];
        for (int i = 0; i < len; i++) {
            res[len - i- 1] = chars[i];
        }

        return String.valueOf(res);
    }

    public static void main(String[] args) {
        System.out.println(new ReverseWords().solution("let's go"));
    }
}
